package com.example.springbootjpasqllogdemo.controller;

import com.example.springbootjpasqllogdemo.common.result.Result;
import com.example.springbootjpasqllogdemo.dto.UserRecordListDTO;
import com.example.springbootjpasqllogdemo.dto.UserRecordSaveOrUpdateDTO;
import com.example.springbootjpasqllogdemo.service.UserRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 系统用户档案表 前端控制层
 *
 * @author Lv
 * @date 2022/11/4
 */
@RestController
@RequestMapping("/user_record")
public class UserRecordController {

    @Autowired
    private UserRecordService userRecordService;

    /**
     * 保存用户档案数据
     *
     * @param userRecordSaveOrUpdateDTO 用户档案添加对象
     * @return
     */
    @PostMapping("/")
    public Result<?> save(@Validated @RequestBody UserRecordSaveOrUpdateDTO userRecordSaveOrUpdateDTO) {
        return userRecordService.save(userRecordSaveOrUpdateDTO);
    }

    /**
     * 分页查询用户档案列表
     *
     * @param userRecordListDTO 用户档案查询对象
     * @return
     */
    @GetMapping("/")
    public Result<?> load(@RequestBody UserRecordListDTO userRecordListDTO) {
        return userRecordService.load(userRecordListDTO);
    }

    /**
     * 获取详情
     *
     * @param id 用户档案id
     * @return
     */
    @GetMapping("/{id}")
    public Result<?> getById(@PathVariable("id") Integer id) {
        return userRecordService.getById(id);
    }

    /**
     * 修改用户档案数据
     *
     * @param id                        用户档案id
     * @param userRecordSaveOrUpdateDTO 用户档案修改对象
     */
    @PutMapping("/{id}")
    public Result<?> update(@PathVariable("id") Integer id, @Validated @RequestBody UserRecordSaveOrUpdateDTO userRecordSaveOrUpdateDTO) {
        return userRecordService.update(id, userRecordSaveOrUpdateDTO);
    }

    /**
     * 删除用户档案
     *
     * @param id 用户档案id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable("id") Integer id) {
        return userRecordService.delete(id);
    }
}
